package com.ssm.demo.service.tbuser.service.impl;

import com.ssm.demo.service.dao.UserMapper;
import com.ssm.demo.service.tbuser.bean.User;
import com.ssm.demo.service.tbuser.bean.UserExample;
import com.ssm.demo.service.tbuser.service.IUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User addOne(User bean) {
        bean.setId(UUID.randomUUID().toString());
        int insert = userMapper.insert(bean);
        return insert > 0 ? bean : null;
    }

    @Override
    public List<User> addBatch(List<User> beans) {
        return null;
    }

    @Override
    public void removeById(String id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void removeByIds(List<String> ids) {
        UserExample example = new UserExample();
        example.createCriteria().andIdIn(ids);
        userMapper.deleteByExample(example);
    }

    @Override
    public void modifyById(User bean) {
        userMapper.updateByPrimaryKey(bean);
    }

    @Override
    public void modifyBatchById(List<User> beans) {

    }

    @Override
    public User getById(String id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user == null ? null : user;
    }

    @Override
    public List<User> getByIds(List<String> ids) {
        UserExample example = new UserExample();
        example.createCriteria().andIdIn(ids);
        List<User> userList = userMapper.selectByExample(example);
        return CollectionUtils.isEmpty(userList) ? Collections.EMPTY_LIST : userList;
    }

}
